jQuery(document).on("pageinit", "[data-role='page'][data-pagetype='myprofilemod']", function() {
    var page = jQuery(this);

    var email = page.find("input#email");
    var nickname = page.find("input#nickname");
    var name = page.find("input#name");
    var address = page.find("input#address");
    var phoneno = page.find("input#phoneno");
    var gender = page.find("select#gender");
    var password = page.find("input#password");


    $.getJSON(URL_ACTION_MY_PROFILE, {email: $.cookie("email")}, function (result) {
        email.val(result.email);
        nickname.val(result.nickname);
        name.val(result.name);
        address.val(result.address);
        phoneno.val(result.phoneno);
        password.val($.cookie("wia_password")); 
        gender.val(result.gender).attr("selected", "selected");
        gender.slider("refresh");
    });

    page.find("a.save").click(function(event) {
        $.ajax({
            type: "POST",
            url: URL_ACTION_MODIFY_MY_PROFILE,
            data: {
                email: email.val(),
                nickname: nickname.val(),
                name: name.val(),
                address: address.val(),
                phoneno: phoneno.val(),
                gender: gender.val(),
                password: password.val()
            },

            success: function(data, status) {
                $.cookie("wia_email", email.val());
                $.cookie("wia_password", password.val());
                $.mobile.changePage(URL_VIEW_SERVICES, { transition: "slide", reverse: false, changeHash: true });
            },

            error: function(req, status, error) {
                var errmsg = page.find("div.errmsg");
                errmsg.find("p").html("修改用户信息失败！");
                errmsg.popup("open");
            }
        });
    });
});
